package com.lsl.bhllserver.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsl.bhllserver.dao.func.IDelete;
import com.lsl.bhllserver.dao.func.IInsert;
import com.lsl.bhllserver.dao.func.IQuery;
import com.lsl.bhllserver.dao.func.IUpdate;
import com.lsl.bhllserver.dao.template.DAOTemplate;
import com.lsl.bhllserver.entity.Product;
import com.lsl.bhllserver.mapper.ProductMapper;

@Repository
public class ProductDAO implements DAOTemplate<Product>{

    @Autowired
    private ProductMapper productMapper;

    @Override
    public boolean insert(Object product) {
        IInsert insertor = (prodtemp) -> {
            return productMapper.insert((Product)prodtemp) > 0;
        };
        try{
            return insertor.insert(product);
        } catch (Exception e) {
            System.out.println("[ProductDAO] +++++ insert 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
        
    }

    @Override
    public boolean update(Integer id, Object product) {
        IUpdate updater = (idtemp, prodtemp) -> {
            Product prodtemp2 = (Product)prodtemp;
            prodtemp2.setId(id);
            return productMapper.updateById((Product)prodtemp2) > 0;
        };
        try{
            return updater.update(id, product);
        } catch (Exception e) {
            System.out.println("[ProductDAO] +++++ update 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer id) {
        IDelete deletor = (pid) -> {
            return productMapper.deleteById(id) > 0;
        };
        try{
            return deletor.delete(id);
        } catch (Exception e) {
            System.out.println("[ProductDAO] +++++ delete 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Product> getAll() {
        IQuery<Product> queryor = (kw) -> {
            return productMapper.selectList(null);
        };
        try{
            return queryor.query(null);
        } catch (Exception e) {
            System.out.println("[ProductDAO] +++++ getAll 的 Lambda 方法出错：");
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Product findByID(String id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<Product> findByKeywords(String[] keywords) {
        IQuery<Product> queryor = (kw) -> {
            QueryWrapper<Product> qw = new QueryWrapper<>();
            qw.like("id", kw[0]);
            qw.like("prod_name", kw[1]);
            qw.like("bus_id", kw[2]);
            return productMapper.selectList(qw);
        };
        try{
            return queryor.query(keywords);
        } catch (Exception e) {
            System.out.println("[ProductDAO] +++++ findByKeywords 的 Lambda 方法出错：");
            e.printStackTrace();
            return null;
        }
    }

    // 新增加方法：根据商家ID查询商品
    public List<Product> findByBusID(Integer busid) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("bus_id", busid);
        return productMapper.selectList(qw);
    }

}
